package com.ruoyi.web.controller.shop.service.Impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.entity.DO.member.MemberDO;
import com.ruoyi.common.entity.DO.member.MemberLabelDO;
import com.ruoyi.common.entity.DO.member.MemberLabelLinkDO;
import com.ruoyi.common.entity.VO.member.MemberLabelSearchVO;
import com.ruoyi.common.entity.VO.member.MemberLabelVO;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.web.controller.member.mapper.IBackMemberMapper;
import com.ruoyi.web.controller.shop.mapper.IBackShopMemberLabelLinkMapper;
import com.ruoyi.web.controller.shop.mapper.IBackShopMemberLabelMapper;
import com.ruoyi.web.controller.shop.service.IBackShopMemberLabelService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.poi.util.ArrayUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Member;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
public class BackShopMemberLabelServiceImpl implements IBackShopMemberLabelService {

    @Resource
    private IBackShopMemberLabelMapper backShopMemberLabelMapper;

    @Resource
    private IBackShopMemberLabelLinkMapper backShopMemberLabelLinkMapper;

    @Resource
    private IBackMemberMapper backMemberMapper;


    @Override
    public List<MemberLabelVO> searchList(MemberLabelSearchVO searchVO) {
        searchVO.setIsDelete(0);
        return backShopMemberLabelMapper.searchList(searchVO);
    }

    @Override
    public String checkLabelNameUnique(MemberLabelSearchVO memberLabelSearchVO) {
        Long labelId = StringUtils.isNull(memberLabelSearchVO.getLabelId()) ? -1L : memberLabelSearchVO.getLabelId();
        MemberLabelDO info = backShopMemberLabelMapper.checkLabelNameUnique(memberLabelSearchVO.getLabelName(), memberLabelSearchVO.getShopId());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != labelId.longValue()) {
            return Constants.FAIL;
        }
        return Constants.SUCCESS;
    }

    @Override
    public void add(MemberLabelVO memberLabelVO) {
        MemberLabelDO newLabel = new MemberLabelDO();
        BeanUtils.copyProperties(memberLabelVO, newLabel);
        newLabel.setCreateTime(new Date());
        backShopMemberLabelMapper.insertSelective(newLabel);
    }

    @Override
    @Transactional
    public void edit(MemberLabelVO memberLabelVO) {
        MemberLabelDO memberLabelInfo = backShopMemberLabelMapper.selectByPrimaryKey(memberLabelVO.getId());
        if (memberLabelInfo == null || memberLabelInfo.getIsDelete() == 1) {
            throw new BusinessException("没找到该会员标签！");
        }
        String oldLabelName = memberLabelInfo.getLabelName();
        if (null != memberLabelVO.getLabelName()) {
            memberLabelInfo.setLabelName(memberLabelVO.getLabelName());
        }
        if (null != memberLabelVO.getOrderNum()) {
            memberLabelInfo.setOrderNum(memberLabelVO.getOrderNum());
        }
        if (null != memberLabelVO.getIsEnable()) {
            memberLabelInfo.setIsEnable(memberLabelVO.getIsEnable());
        }
        memberLabelInfo.setUpdateBy(memberLabelVO.getUpdateBy());
        memberLabelInfo.setUpdateTime(new Date());
        backShopMemberLabelMapper.updateByPrimaryKeySelective(memberLabelInfo);
        if(null != memberLabelVO.getLabelName() && !oldLabelName.equals(memberLabelInfo.getLabelName())){
            //TODO:重置包含该标签的全部会员标签信息
            List<Long> linkMemberIds = backShopMemberLabelLinkMapper.searchMemberIdList(memberLabelInfo.getId());
            if(!linkMemberIds.isEmpty()){
                //刷新会员标签信息
                for (Long memberId:linkMemberIds
                ) {
                    MemberDO memberDO = backMemberMapper.selectByPrimaryKey(memberId);
                    String[] tmpSet = memberDO.getLabelNames().split(",");
                    for (int i=0;i<tmpSet.length;i++){
                        if(tmpSet[i].equals(oldLabelName)){
                            tmpSet[i] = memberLabelInfo.getLabelName();
                        }
                    }
                    memberDO = new MemberDO();
                    memberDO.setId(memberId);
                    memberDO.setLabelNames(String.join(",",tmpSet));
                    backMemberMapper.updateByPrimaryKeySelective(memberDO);
                }
            }
        }
    }

    @Override
    public MemberLabelDO selectById(Long id) {
        return backShopMemberLabelMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int removeByIds(String ids) {
        Long id = Long.parseLong(ids);
        MemberLabelDO memberLabelInfo = backShopMemberLabelMapper.selectByPrimaryKey(id);
        if (memberLabelInfo == null || memberLabelInfo.getIsDelete() == 1) {
            throw new BusinessException("没找到该会员标签！");
        }
        //TODO:重置包含该标签的全部会员标签信息
        List<Long> linkMemberIds = backShopMemberLabelLinkMapper.searchMemberIdList(id);
        if(!linkMemberIds.isEmpty()){
            //刷新会员标签信息
            for (Long memberId:linkMemberIds
            ) {
                MemberDO memberDO = backMemberMapper.selectByPrimaryKey(memberId);
                String[] tmpSet = memberDO.getLabelNames().split(",");
                int idx = -1;
                for (int i=0;i<tmpSet.length;i++){
                    if(tmpSet[i].equals(memberLabelInfo.getLabelName())){
                        idx = i;
                        break;
                    }
                }
                if(idx>-1){
                    ArrayUtils.remove(tmpSet,idx);
                }
                memberDO = new MemberDO();
                memberDO.setId(memberId);
                memberDO.setLabelNames(String.join(",",ArrayUtils.remove(tmpSet,idx)));
                backMemberMapper.updateByPrimaryKeySelective(memberDO);
            }
        }
        backShopMemberLabelLinkMapper.deleteByLabelId(id);
        return backShopMemberLabelMapper.deleteByIds(ids);
    }

    @Override
    @Transactional
    public void link(MemberLabelVO memberLabelVO) {
        MemberLabelLinkDO memberLabelLinkDO;
        MemberDO memberInfo = backMemberMapper.selectByPrimaryKey(memberLabelVO.getMemberId());
        if(memberInfo == null){
            throw new BusinessException("会员不存在");
        }
        backShopMemberLabelMapper.deleteByMemberId(memberLabelVO.getMemberId());
        String labelNames = "";
        if(StringUtils.isNotEmpty(memberLabelVO.getLabelIdArr())){
            for (Long labelId:memberLabelVO.getLabelIdArr()
            ) {
                labelNames += backShopMemberLabelMapper.getLabelNameById(labelId)+",";
                memberLabelLinkDO = new MemberLabelLinkDO();
                memberLabelLinkDO.setMemberId(memberLabelVO.getMemberId());
                memberLabelLinkDO.setMemberLabelId(labelId);
                backShopMemberLabelLinkMapper.insertSelective(memberLabelLinkDO);
            }
            labelNames = labelNames.substring(0,labelNames.length()-1);
        }
        memberInfo.setLabelNames(labelNames);
        backMemberMapper.updateByPrimaryKeySelective(memberInfo);

    }
}
